package com.ruoyi.qualityControl.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.service.TableService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.qualityControl.domain.*;
import com.ruoyi.qualityControl.domain.vo.QcInfoIndexExamineVo;
import com.ruoyi.qualityControl.service.QcInfoIndexExamineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/qc/infoIndex")
@RestController
@Api(tags = "质控中心_信息指数考核")
public class QcInfoIndexExamineController {
    @Autowired
    private QcInfoIndexExamineService qcInfoIndexExamineService;
    @Autowired
    private TableService tableService;

    @ApiOperation("及时性表")
    @PostMapping("/inTimeSheet")
    public R inTimeSheet(@RequestBody QcInfoIndexExamineVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null ){
            return R.fail("请输入查询所需的必要参数");
        }
        List<QcInfoIndexExamineInTime> sheets = qcInfoIndexExamineService.collectInTime(vo);
        if (sheets == null || sheets.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",PageInfo.info(sheets));
            jsonObject.put("fields",qcInfoIndexExamineService.initTable());
            return R.ok(jsonObject);
        }
    }
    @ApiOperation("精确性表")
    @PostMapping("/integrity")
    public R collectIntegrity(@RequestBody QcInfoIndexExamineVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null ){
            return R.fail("请输入查询所需的必要参数");
        }
        List<QcInfoIndexExamineIntegrity> sheets = qcInfoIndexExamineService.collectIntegrity(vo);
        if (sheets == null || sheets.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",PageInfo.info(sheets));
            String[] fields = {"collectDate", "contractArea", "networkName", "orderAmount", "receiveMissAmount", "receiveMissRate"
                    , "signTotalAmount", "deliveryMissAmount", "deliveryMissRate", "arrMissingCount", "arrMissingPerStr"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("integrity");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

    @ApiOperation("完整性表")
    @PostMapping("/accuracy")
    public R collectAccuracy(@RequestBody QcInfoIndexExamineVo vo){
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null ){
            return R.fail("请输入查询所需的必要参数");
        }
        List<QcInfoIndexExamineAccuracy> sheets = qcInfoIndexExamineService.collectAccuracy(vo);
        if (sheets == null || sheets.size() == 0){
            return R.fail("暂无数据");
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",PageInfo.info(sheets));
            String[] fields = {"collectDate", "contractArea[networkName]", "networkName[networkName]", "receiveTotalAmount", "deliveryTotalAmount", "signTotalAmount"
                    , "scanSingleAmount", "problemAmount", "receiveLateDelivery", "receiveLateSign", "deliveryLateSign", "noAccuracyRate"
                    , "arrAfterSignCount", "arrIssuePerStr"};
            InitTableVo initTableVo = new InitTableVo();
            initTableVo.setFields(fields);
            initTableVo.setCode("accuracy");
            jsonObject.put("fields",tableService.initTable(initTableVo));
            return R.ok(jsonObject);
        }
    }

//    @ApiOperation("及时性汇总")
//    @PostMapping("/collectOne")
//    public R collectOne(@RequestBody QcInfoIndexExamineVo vo) {
//        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null) {
//            return R.fail("请输入查询所需的必要参数");
//        }
//        QcInfoIndexExamineCollect collect = qcInfoIndexExamineService.collectOne(vo);
//        if (collect == null){
//            return R.fail("暂无数据");
//        }else{
//            return R.ok(collect);
//        }
//    }
//    @ApiOperation("完整性汇总")
//    @PostMapping("/collectTwo")
//    public R collectTwo(@RequestBody QcInfoIndexExamineVo vo) {
//        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null) {
//            return R.fail("请输入查询所需的必要参数");
//        }
//        QcInfoIndexExamineIntegrityCollect collect = qcInfoIndexExamineService.collectTwo(vo);
//        if (collect == null){
//            return R.fail("暂无数据");
//        }else{
//            return R.ok(collect);
//        }
//    }
//
//    @ApiOperation("准确性汇总")
//    @PostMapping("/collectThree")
//    public R collectThree(@RequestBody QcInfoIndexExamineVo vo) {
//        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null) {
//            return R.fail("请输入查询所需的必要参数");
//        }
//        QcInfoIndexExamineAccuracyCollect collect = qcInfoIndexExamineService.collectThree(vo);
//        if (collect == null){
//            return R.fail("暂无数据");
//        }else{
//            return R.ok(collect);
//        }
//    }

    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody QcInfoIndexExamineVo vo) {
        if (vo.getStartTime() == null || vo.getEndTime() == null || vo.getNetworkNameId() == null) {
            return R.fail("请输入查询所需的时间信息和网点信息！");
        }
        QcInfoIndexExamineAll all = new QcInfoIndexExamineAll();
//        JSONObject jsonObject = new JSONObject();
        List<QcInfoIndexExamineCards> cards = new ArrayList<>();
        if (vo.getType().equals("")){
            QcInfoIndexExamineCards cards1 = new QcInfoIndexExamineCards();
            cards1.setTittle("及时性");
            cards1.setCardList(qcInfoIndexExamineService.collectOne(vo));
            cards.add(cards1);
            QcInfoIndexExamineCards cards2 = new QcInfoIndexExamineCards();
            cards2.setTittle("完整性");
            cards2.setCardList(qcInfoIndexExamineService.collectTwo(vo));
            cards.add(cards2);
            QcInfoIndexExamineCards cards3 = new QcInfoIndexExamineCards();
            cards3.setTittle("准确性");
            cards3.setCardList(qcInfoIndexExamineService.collectThree(vo));
            cards.add(cards3);
            all.setCards(cards);
            JSONObject data = (JSONObject) inTimeSheet(vo).getData();
            all.setSheets(data);
            return R.ok(all);
        }
        if (vo.getType().equals("inTime")){
            QcInfoIndexExamineCards cards1 = new QcInfoIndexExamineCards();
            cards1.setTittle("及时性");
            cards1.setCardList(qcInfoIndexExamineService.collectOne(vo));
            cards.add(cards1);
            QcInfoIndexExamineCards cards2 = new QcInfoIndexExamineCards();
            cards2.setTittle("完整性");
            cards2.setCardList(qcInfoIndexExamineService.collectTwo(vo));
            cards.add(cards2);
            QcInfoIndexExamineCards cards3 = new QcInfoIndexExamineCards();
            cards3.setTittle("准确性");
            cards3.setCardList(qcInfoIndexExamineService.collectThree(vo));
            cards.add(cards3);
            all.setCards(cards);
            JSONObject data = (JSONObject) inTimeSheet(vo).getData();
            all.setSheets(data);
            return R.ok(all);
        }
        if (vo.getType().equals("accuracy")){
            QcInfoIndexExamineCards cards1 = new QcInfoIndexExamineCards();
            cards1.setTittle("及时性");
            cards1.setCardList(qcInfoIndexExamineService.collectOne(vo));
            cards.add(cards1);
            QcInfoIndexExamineCards cards2 = new QcInfoIndexExamineCards();
            cards2.setTittle("完整性");
            cards2.setCardList(qcInfoIndexExamineService.collectTwo(vo));
            cards.add(cards2);
            QcInfoIndexExamineCards cards3 = new QcInfoIndexExamineCards();
            cards3.setTittle("准确性");
            cards3.setCardList(qcInfoIndexExamineService.collectThree(vo));
            cards.add(cards3);
            all.setCards(cards);
            JSONObject data = (JSONObject) collectAccuracy(vo).getData();
            all.setSheets(data);
            return R.ok(all);
        }
        if (vo.getType().equals("integrity")){
            QcInfoIndexExamineCards cards1 = new QcInfoIndexExamineCards();
            cards1.setTittle("及时性");
            cards1.setCardList(qcInfoIndexExamineService.collectOne(vo));
            cards.add(cards1);
            QcInfoIndexExamineCards cards2 = new QcInfoIndexExamineCards();
            cards2.setTittle("完整性");
            cards2.setCardList(qcInfoIndexExamineService.collectTwo(vo));
            cards.add(cards2);
            QcInfoIndexExamineCards cards3 = new QcInfoIndexExamineCards();
            cards3.setTittle("准确性");
            cards3.setCardList(qcInfoIndexExamineService.collectThree(vo));
            cards.add(cards3);
            all.setCards(cards);
            JSONObject data = (JSONObject) collectIntegrity(vo).getData();
            all.setSheets(data);
            return R.ok(all);
        }

        return R.fail("暂无数据");
    }
}
